package web.get;

import domain.User;
import service.UserService;
import service.impl.UserServiceImpl;
import util.ServletUtil;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;

@WebServlet("/getWordHistory")
public class GetWordHistoryServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request, response);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");
        response.setContentType("application/json;charset=utf-8");

        UserService us = new UserServiceImpl();
        User user = ServletUtil.checkLogin(request);
        if (user==null){
            request.getRequestDispatcher("login.html").forward(request,response);
        }else {
            //查询最新的浏览记录，并转发到/getWord
            String bid = request.getParameter("book");
            if(!"".equals(bid) && bid!=null && !"undefined".equals(bid)){
                request.setAttribute("book",Integer.parseInt(bid));
                request.getRequestDispatcher("/getWord").forward(request,response);
            }else {
                int lastHistory = us.getLastHistory(user.getUid());
                System.out.println("历史记录bid："+lastHistory);
                request.setAttribute("book",lastHistory);
                request.getRequestDispatcher("/getWord").forward(request,response);
            }

        }
    }
}
